package com.farm.kaiping.controller;


import com.farm.kaiping.entity.MmMaterial;
import com.farm.kaiping.po.MmMaterialPO;
import com.farm.kaiping.service.MmMaterialService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farm.common.util.PageParaUtils;
import com.farm.common.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 材料物料的说明与库存(MmMaterial)表控制层
 *
 * @author makejava
 * @since 2025-01-22 14:35:49
 */
@RestController
@RequestMapping("/mmMaterial")
@AllArgsConstructor
@Api(tags = "材料物料的说明与库存控制器")
public class MmMaterialController {

	private final MmMaterialService mmMaterialService;

	@ApiOperation(value = "分页查询材料物料的说明与库存", notes = "权限：view_mmMaterial_page")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_mmMaterial_page')")
	@PostMapping("/page")
	public R<Page<MmMaterial>> getMmMaterialPage(@RequestBody MmMaterialPO mmMaterialPO) {
		try{
		Page page = PageParaUtils.withdrawPageByParamType(mmMaterialPO);
		return R.ok(mmMaterialService.getMmMaterialPage(page, mmMaterialPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询材料物料的说明与库存列表", notes = "权限：view_mmMaterial_list")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_mmMaterial_list')")
	@PostMapping("/list")
	public R<List<MmMaterial>> getMmMaterialList(@RequestBody MmMaterialPO mmMaterialPO) {
		try{
			return R.ok(mmMaterialService.getMmMaterialList(mmMaterialPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "通过id查询材料物料的说明与库存", notes = "权限 view_mmMaterial_view")
	@GetMapping("/{id}" )
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_mmMaterial_view')" )
	public R getById(@PathVariable("id" ) String id) {
		try{
			return R.ok(mmMaterialService.getById(id));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "查询一条材料物料的说明与库存", notes = "权限：view_mmMaterial_one")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_mmMaterial_one')")
	@PostMapping("/one")
	public R<MmMaterial> getMmMaterial(@RequestBody MmMaterialPO mmMaterialPO) {
		try{
			return R.ok(mmMaterialService.getMmMaterial(mmMaterialPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}
	
	@ApiOperation(value = "新增材料物料的说明与库存", notes = "权限：view_mmMaterial_add")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_mmMaterial_add')")
	@PostMapping("/add")
	public R saveMmMaterial(@RequestBody MmMaterialPO mmMaterialPO) {
		try{
		    return R.ok(mmMaterialService.saveMmMaterial(mmMaterialPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}
	}

	@ApiOperation(value = "修改材料物料的说明与库存", notes = "权限：view_mmMaterial_edit")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_mmMaterial_edit')")
	@PostMapping("/edit")
	public R updateMmMaterial(@RequestBody MmMaterialPO mmMaterialPO) {
		try{
		    return R.ok(mmMaterialService.updateMmMaterial(mmMaterialPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}    
	}

	@ApiOperation(value = "通过id删除材料物料的说明与库存", notes = "权限：view_mmMaterial_del")
	@PreAuthorize("@pms.hasPermission('ROLE_1','view_mmMaterial_del')")
	@PostMapping("/del")
	public R removeMmMaterial(@RequestBody MmMaterialPO mmMaterialPO) {
		try{
		    return R.ok(mmMaterialService.removeMmMaterial(mmMaterialPO));
		}catch (Exception e){
			return R.failed(e.getMessage());
		}  
	}
	
}

